<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
require_once(APP . '/application/models/Alllist.php');
/**
 * 后台管理主页
 * @package admin
 */
class PublicationController extends AdminAction
{
    
	/**
	 * Enter description here...
	 *
	 * @var Publication
	 */
	private $publication;
	
	/**
	 * 分类类型
	 *
	 * @var unknown_type
	 */
	private $type;
		
    public function init()
    {
    	global $cfg;
        $this->type = $cfg['publication_list_type'];
        
    	require_once(APP . '/application/models/Publication.php');
    	
        $this->publication = new Publication($this->db);
    }


    /**
     * 加载框架页
     */
    public function addAction()
    {
    	$type = $this->getParam("type");
    	
    	$obj_list = new Alllist($this->db);
	
		$this->view->list = $obj_list->getAllListByType($this->type);
		$this->view->studylist = array();
    	
    	$this->view->type = $type;
    }
    
    public function doaddAction()
    {
    	$type = $this->getParam("type");
    	$params = $this->getAllParams();
    	
    	/**************图片处理*********************/
    	$image = "";
    	if (!empty($_FILES) && !empty($_FILES['image']) && !empty($_FILES['image']['name'])){
    		$file_ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/image'))
    			mkdir($todir . '/image');
    		if (!file_exists($todir . '/image/' . date("Ymd")))
    			mkdir($todir . '/image/' . date("Ymd"));
    		
    		$image = '/upload/image/' . date("Ymd") . '/' .date("YmdHis") . '_' . uniqid("h") . '.' . $file_ext;
    		move_uploaded_file($_FILES['image']['tmp_name'], APP . $image);
    	}
    	$params['image'] = $image;
    	
    	/**************PDF处理*********************/
    	$spdf = "";
    	if (!empty($_FILES) && !empty($_FILES['pdf']) && !empty($_FILES['pdf']['name'])){
    		$file_ext = pathinfo($_FILES['pdf']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/pdf'))
    			mkdir($todir . '/pdf');
    		if (!file_exists($todir . '/pdf/' . date("Ymd")))
    			mkdir($todir . '/pdf/' . date("Ymd"));
    		
    		$spdf = '/upload/pdf/' . date("Ymd") . '/' . urlencode(str_replace(" ", "",pathinfo($_FILES['pdf']['name'], PATHINFO_BASENAME )));
    		$pdf = '/upload/pdf/' . date("Ymd") . '/' . str_replace(" ", "",pathinfo($_FILES['pdf']['name'], PATHINFO_BASENAME ));
			
    		$isWin = !!preg_match("/windows/i", $_SERVER['SystemRoot']);
    		if ($isWin)
    			move_uploaded_file($_FILES['pdf']['tmp_name'], iconv("UTF-8", "GBK", APP . $pdf));
    		else
    			move_uploaded_file($_FILES['pdf']['tmp_name'], APP . $pdf);
    	}
    	$params['pdf'] = $spdf;
    	
    	/**************全文下载处理*********************/
    	$sdown = "";
    	if (!empty($_FILES) && !empty($_FILES['down']) && !empty($_FILES['down']['name'])){
    		$file_ext = pathinfo($_FILES['down']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/file'))
    			mkdir($todir . '/file');
    		if (!file_exists($todir . '/file/' . date("Ymd")))
    			mkdir($todir . '/file/' . date("Ymd"));
    		
    		$sdown = '/upload/file/' . date("Ymd") . '/' . urlencode(str_replace(" ", "",pathinfo($_FILES['down']['name'], PATHINFO_BASENAME )));
    		$down = '/upload/file/' . date("Ymd") . '/' . str_replace(" ", "",pathinfo($_FILES['down']['name'], PATHINFO_BASENAME ));
			
    		$isWin = !!preg_match("/windows/i", $_SERVER['SystemRoot']);
    		if ($isWin)
    			$ret = move_uploaded_file($_FILES['down']['tmp_name'], iconv("UTF-8", "GBK", APP . $down));
    		else
    			$ret = move_uploaded_file($_FILES['down']['tmp_name'], APP . $down);
    	}
    	$params['down'] = $sdown;
    	
    	
    	if ($this->getParam("top") == 1){
    		$params['seq'] = $this->db->fetchOne("select max(seq)+1 from publication");
    	}
    	
    	$this->publication->add($params);
    	
    	$this->view->msg = '添加成功';
    	
    	$this->view->url = $this->url($this->view->baseUrl, "publication", "add", "type=" . $type, "admin");
    	$this->showmessage();
    }
    
    public function listAction()
    {
    	$nowp = (int) $this->getParam("nowp", 1);
    	$type = $this->getParam("type");
    	$per_page = 10;
    	
    	$where = "";
    	$result = $this->publication->listPublicationByWhere($where, $nowp, $per_page);
    	
    	$this->view->nowp = $nowp;
        $this->view->list = $result['list'];
        $this->view->page = $result['page'];
        $this->view->type = $type;
    }
    
    
    public function delAction()
    {
    	$pid = (int) $this->getParam("pid");
    	$nowp = (int) $this->getParam("nowp");
    	$type = $this->getParam("type");
    	
    	$this->publication->del($pid);
    	
    	header("location:" . $this->url($this->view->baseUrl, "publication", "list", "nowp=" . $nowp . '&type=' . $type, "admin"));
    }
    
    public function editAction()
    {
    	$pid = (int) $this->getParam("pid");
    	$nowp = (int) $this->getParam("nowp");
    	$type = $this->getParam("type");
    	$content = $this->publication->show($pid);
    	
    	$obj_list = new Alllist($this->db);
   		$this->view->list = $obj_list->getAllListByType($this->type);
    	
    	$this->view->type = $type;
        $this->view->nowp = $nowp;
        $this->view->content = $content;
    }
    
    public function doeditAction()
    {
    	$pid = (int)$this->getParam("pid");
    	$nowp = (int)$this->getParam("nowp");
    	$type = $this->getParam("type");
    	$params = $this->getAllParams();
    	    	
    	if ($this->getParam("top") == 1){
    		$params['seq'] = $this->db->fetchOne("select max(seq)+1 from publication");
    	}
    	
    	/**************图片处理*********************/
    	$image = "";
    	if (!empty($_FILES) && !empty($_FILES['image']) && !empty($_FILES['image']['name'])){
    		$file_ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/image'))
    			mkdir($todir . '/image');
    		if (!file_exists($todir . '/image/' . date("Ymd")))
    			mkdir($todir . '/image/' . date("Ymd"));
    		
    		$image = '/upload/image/' . date("Ymd") . '/' .date("YmdHis") . '_' . uniqid("h") . '.' . $file_ext;
    		move_uploaded_file($_FILES['image']['tmp_name'], APP . $image);
    	}
    	if (!empty($image))
    		$params['image'] = $image;
    	
    	/**************PDF处理*********************/
    	$spdf = "";
    	if (!empty($_FILES) && !empty($_FILES['pdf']) && !empty($_FILES['pdf']['name'])){
    		$file_ext = pathinfo($_FILES['pdf']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/pdf'))
    			mkdir($todir . '/pdf');
    		if (!file_exists($todir . '/pdf/' . date("Ymd")))
    			mkdir($todir . '/pdf/' . date("Ymd"));
    		
    		$spdf = '/upload/pdf/' . date("Ymd") . '/' . urlencode(str_replace(" ", "",pathinfo($_FILES['pdf']['name'], PATHINFO_BASENAME )));
    		$pdf = '/upload/pdf/' . date("Ymd") . '/' . str_replace(" ", "",pathinfo($_FILES['pdf']['name'], PATHINFO_BASENAME ));

    		$isWin = !!preg_match("/windows/i", $_SERVER['SystemRoot']);
    		if ($isWin)
    			move_uploaded_file($_FILES['pdf']['tmp_name'], iconv("UTF-8", "GBK", APP . $pdf));
    		else
    			move_uploaded_file($_FILES['pdf']['tmp_name'], APP . $pdf);
    	}
    	if (!empty($spdf))
    		$params['pdf'] = $spdf;
    		
    	/**************全文下载处理*********************/
    	$sdown = "";
    	if (!empty($_FILES) && !empty($_FILES['down']) && !empty($_FILES['down']['name'])){
    		$file_ext = pathinfo($_FILES['down']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/file'))
    			mkdir($todir . '/file');
    		if (!file_exists($todir . '/file/' . date("Ymd")))
    			mkdir($todir . '/file/' . date("Ymd"));
    		
    		$sdown = '/upload/file/' . date("Ymd") . '/' . urlencode(str_replace(" ", "",pathinfo($_FILES['down']['name'], PATHINFO_BASENAME )));
    		$down = '/upload/file/' . date("Ymd") . '/' . str_replace(" ", "",pathinfo($_FILES['down']['name'], PATHINFO_BASENAME ));
			
    		$isWin = !!preg_match("/windows/i", $_SERVER['SystemRoot']);
    		if ($isWin)
    			$ret = move_uploaded_file($_FILES['down']['tmp_name'], iconv("UTF-8", "GBK", APP . $down));
    		else
    			$ret = move_uploaded_file($_FILES['down']['tmp_name'], APP . $down);
    	}
    	if (!empty($sdown))
    		$params['down'] = $sdown;
    	
    	$this->publication->change($pid, $params);
    	
    	$this->view->msg = '编辑成功';
    	$this->view->url = $this->url($this->view->baseUrl, "publication", "list", "nowp=" . $nowp . '&type=' . $type, "admin");
    	$this->showmessage();
    }
}